﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;

namespace SharpSoft.WPF.Controls
{
    public class GeoPainter : Canvas
    {
        public GeoPainter()
        {
            this.ClipToBounds = true;
        }

        protected override void OnVisualChildrenChanged(DependencyObject visualAdded, DependencyObject visualRemoved)
        {
            base.OnVisualChildrenChanged(visualAdded, visualRemoved);
            if (visualAdded is GeoControl gcadded)
            {
                gcadded.IsActivated = true;
            }
            if (visualRemoved is GeoControl gcremoved)
            {
                gcremoved.IsActivated = false;
            }
        }
        /// <summary>
        /// 获取子控件所代表的几何图形的原点位置
        /// </summary>
        /// <param name="geoControl"></param>
        /// <returns></returns>
        public Point GetOriginalPosition(GeoControl geoControl)
        {
            var x = Canvas.GetLeft(geoControl);
            var y = Canvas.GetTop(geoControl);
            if (geoControl.Designer == null)
            {
                return new Point(x, y);
            }
            else
            {
                return new Point(x + geoControl.Designer.OriginalPoint.X, y + geoControl.Designer.OriginalPoint.Y);
            }
        }
        /// <summary>
        /// 设置控件原点位置（将控件移动到指定位置并将其内设计器原点设为[0,0]）
        /// </summary>
        /// <param name="geoControl"></param>
        /// <param name="p"></param>
        public void SetOriginalPosition(GeoControl geoControl, Point p)
        {
            Canvas.SetLeft(geoControl, p.X);
            Canvas.SetTop(geoControl, p.Y);
            if (geoControl.Designer == null)
            {
                return;
            }
            else
            {
                geoControl.Designer.OriginalPoint = new Point();
            }
        }

    }
}
